package com.company.struct;

import com.company.TreeNode;

/**
 * @author jrue
 * @description https://leetcode-cn.com/problems/symmetric-tree/
 * @date 2019/11/28 8:39
 * 给定一个二叉树，检查它是否是镜像对称的。
 *
 * 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。
 *     1
 *    / \
 *   2   2
 *  / \ / \
 * 3  4 4  3
 *
 * 2 3 4
 * 2 4 3
 */
public class SymmetricTree {

    public static void main(String[] args) {
        TreeNode node = new TreeNode(1);
        node.left = new TreeNode(2);
        node.right = new TreeNode(2);
        node.left.left = new TreeNode(3);
        node.left.right = new TreeNode(4);
        node.right.left = new TreeNode(4);
        node.right.right = new TreeNode(3);

        System.out.println(new SymmetricTree().isSymmetric(node));
    }

    // 1 2 3 4
    // 1 2 3 4

    public boolean isMirror(TreeNode root1,TreeNode root2) {
        if (root1 == null && root2 == null) return true;
        else if (root1 == null || root2 == null) return false;

        return root1.val == root2.val && isMirror(root1.right,root2.left) && isMirror(root1.left,root2.right);

    }


    public boolean isSymmetric(TreeNode root) {

        return isMirror(root,root);
    }


}
